package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.comment.common.dro.cmt.CommentMessageDRO;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.manager.mq.constant.MqTopicConsts;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.services.interfaces.datasync.ElasticSearchDataSyncService;
import com.zmn.oms.services.interfaces.order.OrderChangeService;
import com.zmn.oms.task.es.business.interfaces.ordercomment.SyncEsOrderCommentBService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类描述：工单
 *
 * @author lujia
 * @date 2018/11/30 16:53
 */
@AliyunMQListener(topic = "${spring.mq.topic.comment}")
@Component
public class OrderCommentChangeListener {

	private static final Logger logger = LoggerFactory.getLogger(OrderCommentChangeListener.class);

	@Autowired
	private ElasticSearchDataSyncService elasticSearchDataSyncService;

	@Autowired
	private OrderChangeService orderChangeService;

	@AliyunMQHandler(tags = OmsMqTagConsts.COMMENT_ADD_MQ_TAG)
	public void orderChange(String message) {
		try {
			logger.info("#mq#ordercomment#recive message:{}", message);
			List<CommentMessageDRO> commentMessageDROS = JSON.parseArray(message, CommentMessageDRO.class);

			List<Long> orderIdList = commentMessageDROS.stream().map(CommentMessageDRO::getOrderId).collect(Collectors.toList());
			elasticSearchDataSyncService.syncWorkIndex(orderIdList);
			for (Long orderId : orderIdList) {
				orderChangeService.changeSyncToMos(orderId);
			}
		} catch (Exception e) {
			logger.error("com.zmn.oms.task.listener.OrderCommentChangeListener.orderChange error={}", e.getMessage());
			throw e;
		}
	}
}
